<?php

require_once 'phpWebHacks.inc';
require_once 'simple_html_dom.inc';

// Function get HTML block by phpWebHack
function getHTML($url){
    $browser    = new phpWebHacks();
    $response   = $browser->get($url);
    $response   = str_get_html($response);
    return $response;
}

// Function get attribute from HTML tag
function getAttrTag($tag, $attr){
    $re = '/'.$attr.'=["\']?([^"\' ]*)["\' ]/is';
    preg_match($re, $tag, $match);
    if($match){
        return urldecode($match[1]);
    }else{
        return false;
    }
}

// Function get image from URL
function getImage($url, $ext, $path){
    $content = file_get_contents($url);
    $fName  = rand(1,1000) . "_" . time() . "." . $ext;
    $fPath  = $_SERVER['DOCUMENT_ROOT'] . $path . $fName;
    
    $handle = fopen($fPath, "w");
    fwrite($handle, $content);
    fclose($handle);
    
    return $fName;
}

// Remove Script & Style tags
function removeHTML($string){
	$string = preg_replace('/<script.*?\>.*?<\/script>/si', ' ', $string);
	$string = preg_replace('/<style.*?\>.*?<\/style>/si', ' ', $string);
	return $string;
}

// Remove Img tags
function removeImg($string){
	$string = preg_replace('/<img.*?\>/si', '', $string);
	return $string;
}

// Remove white space
function removeExcessSpace($data){
	$data  = preg_replace('/(\n|\r)+/','',$data); // Xóa ký tự xuống dòng
	$data  = trim(preg_replace("/[ \s]+/", ' ', $data)); // Xóa ký tự trắng thừa
	return $data;
}

// Remove ky tu dac biet
function removeQuote($string){
	$string = trim($string);
	$string = str_replace("\'", "'", $string);
	//$string = str_replace("'", "''", $string);
	return $string;
}

// Cut a part of string
function cut_string($str, $length, $char=" ..."){
	//Nếu chuỗi cần cắt nhỏ hơn $length thì return luôn
	$strlen	= mb_strlen($str, "UTF-8");
	if($strlen <= $length) return $str;
	
	//Cắt chiều dài chuỗi $str tới đoạn cần lấy
	$substr	= mb_substr($str, 0, $length, "UTF-8");
	if(mb_substr($str, $length, 1, "UTF-8") == " ") return $substr . $char;
	
	//Xác định dấu " " cuối cùng trong chuỗi $substr vừa cắt
	$strPoint= mb_strrpos($substr, " ", "UTF-8");
	
	//Return string
	if($strPoint < $length - 20) return $substr . $char;
	else return mb_substr($substr, 0, $strPoint, "UTF-8") . $char;
}

function htmlspecialbo($str){
	$arrDenied	= array('<', '>', '\"', '"');
	$arrReplace	= array('&lt;', '&gt;', '&quot;', '&quot;');
	$str = str_replace($arrDenied, $arrReplace, $str);
	return $str;
}

// Get the biggest
function getBiggestNum($arr = array()){
    $biggest = $arr[0];
    for($i=0; $i<count($arr); $i++){
        $arr[$i] = trim($arr[$i]);
        $arr[$i] = intval($arr[$i]);
        if($arr[$i] > $biggest){
            $biggest = $arr[$i];
        }
    }
    return $biggest;
}

// Write log
function attentionLog($content){
    $path = $_SERVER['DOCUMENT_ROOT'] . "/get_content/log/";
    $filename = date("Y_m_d_H") . "h.txt";
    
    $handle = @fopen($path . $filename, "a");
    //Nếu handle chưa có mở thêm ../
    if(!$handle) $handle = @fopen($path . $filename, "a");
    //Nếu ko mở đc lần 2 thì exit luôn
    if(!$handle) exit();
    
    //fwrite($handle, date("d/m/Y h:i:S A") . "\n");
    @fwrite($handle, $content);
    @fclose($handle);
}
?>
